import { getComment, getCommentSearch, getCommentPass, getCommentBack, getCommentContext, getCommentDelete } from '@/service';
import { Icomment } from '@/types';
import { Effect, Reducer } from 'umi';

export interface CommentModelState {
    CommentList: Icomment[],
    CommentLength: number,
    CommentPassOrback: any,
    CommentContent: object,
    CommentDelete: object,
}

export interface CommentModelType {
    namespace: 'comment';
    state: CommentModelState;
    effects: {
        getComment: Effect;
        getCommentSearch: Effect;
        getCommentPass: Effect,
        getCommentBack: Effect,
        getCommentContext: Effect,
        getCommentDelete: Effect,
    };
    reducers: {
        save: Reducer<CommentModelState>;
    };
}

const CommemtModel: CommentModelType = {
    namespace: 'comment',
    state: {
        //评论列表数据
        CommentList: [],
        CommentLength: 0,
        CommentPassOrback: {},
        CommentContent: {},
        CommentDelete: {},
    },
    effects: {
        // 评论
        *getComment({ payload }, { call, put }) {
            const result = yield call(getComment, payload)
            console.log(result.data, '评论数据')
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        CommentList: result.data[0],
                        CommentLength: result.data[1]
                    }
                })
            }
        },
        // 搜索
        *getCommentSearch({ payload }, { call, put }) {
            const result = yield call(getCommentSearch, payload)
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        CommentList: result.data[0],
                        CommentLength: result.data[1]
                    }
                })
            }
        },
        //   通过
        *getCommentPass({ payload }, { call, put }) {
            const result = yield call(getCommentPass, payload)
            console.log(payload);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        CommentPassOrback: result
                    }
                })
            }
        },
        //   拒绝
        *getCommentBack({ payload }, { call, put }) {
            const result = yield call(getCommentBack, payload)
            console.log(payload);

            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        CommentPassOrback: result
                    }
                })
            }
        },
        //   回复
        *getCommentContext({ payload }, { call, put }) {
            const result = yield call(getCommentContext, payload)
            console.log(payload);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        CommentContent: result
                    }
                })
            }
        },
        //   删除
        *getCommentDelete({ payload }, { call, put }) {
            const result = yield call(getCommentDelete, payload)
            console.log(payload);
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        CommentDelete: result
                    }
                })
            }
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default CommemtModel;